<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #main {
            width: 500px;
            height: 500px;
            margin: 50px auto;
        }

        li {
            list-style: none;
        }

        #talks li {
            background-color: pink;
            color: #f00;
        }

        #btn {
            margin: 30px 100px;
        }
    </style>
</head>

<body>
    <div id="main">
        <textarea name="" id="talk" cols="50" rows="5">请输入</textarea>
        <br>
        <input id="btn" type="button" value="发布">
        <ul id="talks">
            <li>123</li>
        </ul>
    </div>

    <script>
        var txt = document.querySelector('#talk');
        //文本框获得焦点，清除提示信息 
        txt.onfocus = function () {
            this.value = '';
        }
        //文本框失去焦点，没有内容，还原提示信息
        txt.onblur = function () {
            if (!this.value) {
                this.value = '请输入';
            }
        }
        var btn = document.querySelector('#btn')
        //给按钮绑定单击事件
        btn.onclick = function () {
            //新建元素对象
            var li = document.createElement('li');
            //设置元素对象的内容
            li.innerHTML = txt.value;
            //将元素对象添加到列表中
            var ul = document.querySelector('#talks');
            ul.appendChild(li);
            // ul.insertBefore(li,ul.children[1])
        }
    </script>
</body>

</html>